 

package com.zdkj.subway.support.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.support.entity.SwPmTicketArea;
import com.zdkj.subway.support.service.SwPmTicketAreaService;
import com.zdkj.subway.support.util.UploadDataListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * 票区类型参数表
 *
 * @author cuilei
 * @date 2020-06-01 14:15:55
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/swpmticketarea" )
@Api(value = "swpmticketarea", tags = "票区类型参数表管理")
public class SwPmTicketAreaController {

    private final SwPmTicketAreaService swPmTicketAreaService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param swPmTicketArea 票区类型参数表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getSwPmTicketAreaPage(Page page, SwPmTicketArea swPmTicketArea) {
        return R.ok(swPmTicketAreaService.page(page, Wrappers.query(swPmTicketArea)));
    }

    /**
     * 通过id查询票区类型参数表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(swPmTicketAreaService.getById(id));
    }

    /**
     * 新增票区类型参数表
     * @param swPmTicketArea 票区类型参数表
     * @return R
     */
    @ApiOperation(value = "新增票区类型参数表", notes = "新增票区类型参数表")
    @SysLog("新增票区类型参数表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('support_sw_pm_ticket_area_add')" )
    public R save(@RequestBody SwPmTicketArea swPmTicketArea) {
        SwPmTicketArea defaultObj = ObjectUtils.getDefaultObj(SwPmTicketArea.class);
        BeanUtils.copyProperties(swPmTicketArea, defaultObj,
                ObjectUtils.getNullAndEmptyPropertyNames(swPmTicketArea));
        return R.ok(swPmTicketAreaService.save(defaultObj));
    }

    /**
     * 修改票区类型参数表
     * @param swPmTicketArea 票区类型参数表
     * @return R
     */
    @ApiOperation(value = "修改票区类型参数表", notes = "修改票区类型参数表")
    @SysLog("修改票区类型参数表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('support_sw_pm_ticket_area_edit')" )
    public R updateById(@RequestBody SwPmTicketArea swPmTicketArea) {
        return R.ok(swPmTicketAreaService.updateById(swPmTicketArea));
    }

    /**
     * 通过id删除票区类型参数表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除票区类型参数表", notes = "通过id删除票区类型参数表")
    @SysLog("通过id删除票区类型参数表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('support_sw_pm_ticket_area_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(swPmTicketAreaService.removeById(id));
    }

    /**
     * 票区名称字典
     * @return
     */
    @ApiOperation(value = "票区名称字典", notes = "票区名称字典")
    @SysLog("票区名称字典")
    @GetMapping("/dict")
    public R getDict() {
        QueryWrapper<SwPmTicketArea> queryWrapper = Wrappers.query();
        queryWrapper.orderByAsc("ORDERNO");
        return R.ok(swPmTicketAreaService.list(queryWrapper));
    }

	/**
     * 票区名称字典
     * @return
     */
    @ApiOperation(value = "票区名称字典", notes = "票区名称字典")
    //@SysLog("票区名称字典")
    @GetMapping("/dictCustom")
    public R getDictCustom() {
		java.util.List<SwPmTicketArea> list = swPmTicketAreaService.list();
        SwPmTicketArea custom = new SwPmTicketArea();
		custom.setId(0);
        custom.setTicketAreaName("未设置");
		list.add(0,custom);

        return R.ok(list);
    }

    /**
     * 导入票区类型参数
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "导入票区类型参数", notes = "导入票区类型参数")
    @SysLog("导入票区类型参数")
    @PostMapping("/import")
    public R importExcel(@RequestParam("file") MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), SwPmTicketArea.class,
                    new UploadDataListener(swPmTicketAreaService, SwPmTicketArea.class)).sheet().doRead();
            return R.ok("导入成功");
        } catch (IOException e) {
            log.info(e.getMessage());
            return R.ok("导入失败");
        }
    }
}
